﻿function SPMenu(elementId) {
    var node = document.getElementById(elementId);
    if (node != null) {
        this.RootElementId = elementId;
        this.LargeIconMode = node.getAttribute('largeiconmode') == 'true';
        this.Items = new Array();

        var j = 0;
        var subMenus = node.getElementsByTagName("IE:MENUITEM");
        for (var i = 0; i < subMenus.length; i++) {
            if (subMenus[i].nodeType == 1) {
                if (subMenus[i].getAttribute('type') != null) {
                    this.Items[j] = new SPMenuItem(subMenus[i]);
                    j++;
                }
            }
        }
    }
    else {
        node = document.createElement("menu");
        node.setAttribute("id", elementId);
        node.setAttribute("type", "ServerMenu");
        node.setAttribute("class", "ms-SrvMenuUI");
        node.setAttribute("largeiconmode", "false");
        document.body.appendChild(node);
        return new SPMenu(elementId);
    }
};

SPMenu.prototype.Serialize = function () {
    var res = '<menu largeiconmode="' + this.LargeIconMode + '" type=\'ServerMenu\' class=\'ms-SrvMenuUI\' id=\''+this.RootElementId +'\'>';
    for (var i = 0; i < this.Items.length; i++) { res += this.Items[i].Serialize(); }
    res += '</menu>';
    return res;
};

SPMenu.prototype.Update = function () {
    var menuNode = document.getElementById(this.RootElementId);
    menuNode.setAttribute('largeiconmode', this.LargeIconMode);
    var html = '';
    menuNode.innerHTML = '';
    for (var i = 0; i < this.Items.length; i++) {
        if (!this.Items[i].Hidden) {
            html += this.Items[i].Serialize();
        }
    }
    menuNode.innerHTML = html;
};

function SPMenuItem(node) {
    if (node) {
        this.Id = node.getAttribute('id');
        this.Title = node.getAttribute('text');
        this.Description = node.getAttribute('description');
        this.Type = node.getAttribute('type');
        this.IconSrc = node.getAttribute('iconsrc');
        this.OnClick = node.getAttribute('onmenuclick');
        this.Enabled = node.getAttribute('enabled') != 'false';
        this.IsCheck = node.getAttribute('checked') != null;
        this.Checked = node.getAttribute('checked') == 'true';
        var _ev = node.getAttribute('hidden');
        if (_ev == null)
            _ev = "false";
        this.Hidden = eval(_ev); // == 'true';
        this.Items = new Array();
        var j = 0;
        var subMenus = node.getElementsByTagName("IE:MENUITEM");
        for (var i = 0; i < subMenus.length; i++) {
            if (subMenus[i].nodeType == 1) {
                if (subMenus[i].getAttribute('type') != null) {
                    this.Items[j] = new SPMenuItem(subMenus[i]);
                    j++;
                }
            }
        }
    }
    else {
        this.Id = null;
        this.Title = null;
        this.Description = null;
        this.Type = null;
        this.IconSrc = null;
        this.OnClick = null;
        this.Enabled = true;
        this.Checked = true;
        this.IsCheck = false;
        this.Hidden = false;
        this.Items = new Array();
    }
};

SPMenuItem.prototype.Serialize = function () {
    if (this.Hidden)
        return '';
    var res = '<ie:menuitem';
    if (this.Id)
        res += ' id="' + this.Id + '"';
    if (this.Title)
        res += ' text="' + this.Title + '"';
    if (this.Description)
        res += ' description="' + this.Description + '"';
    if (this.Type)
        res += ' type="' + this.Type + '"';
    if (this.IconSrc)
        res += ' iconsrc="' + this.IconSrc + '"';
    if (this.OnClick)
        res += ' onmenuclick="' + unescape(this.OnClick) + '"';
    res += ' enabled="' + this.Enabled + '">';
    if (this.Items.length > 0) {
        for (var i = 0; i < this.Items.length; i++) {
            res += this.Items[i].Serialize();
        }
    }
    res += '</ie:menuitem>';
    return res;
};

SPMenu.prototype.AddItem = function (menuItem) {
    this.Items.push(menuItem);
}

function SPMenuInit(arg, context) {
    var menuId = context + "_menu";
    var containerId = context + "_Container";
    var tempNode = document.createElement("span");
    tempNode.innerHTML = arg;
    var node = document.getElementById(menuId);
    if (node == null) {
        node = document.createElement("menu");
        node.setAttribute("id", menuId);
        node.setAttribute("type", "ServerMenu");
        node.setAttribute("class", "ms-SrvMenuUI");
        node.setAttribute("largeiconmode", "false");
        document.body.appendChild(node);
    }
    node.innerHTML = tempNode.getElementsByTagName("MENU")[0].innerHTML;
    OpenWebPartMenu(menuId, document.getElementById(containerId), 'False');
    TrapMenuClick(event);
    return false;
    //return new SPMenu(elementId);
}